package com.example.wishstar.view.fragment;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.example.wishstar.MainActivity;
import com.example.wishstar.MyApplication;
import com.example.wishstar.R;
import com.example.wishstar.databinding.FragmentWishBinding;
import com.example.wishstar.model.CommonDate;
import com.example.wishstar.model.Habit;
import com.example.wishstar.model.UpdateHabit;
import com.example.wishstar.model.UpdateWish;
import com.example.wishstar.model.Wish;
import com.example.wishstar.view.CustomOperateDialog;
import com.example.wishstar.view.UpdateHabitDialogFragment;
import com.example.wishstar.view.UpdateWishDialogFragment;
import com.example.wishstar.view.activity.AddHabitActivity;
import com.example.wishstar.view.activity.AddWishActivity;
import com.example.wishstar.view.activity.LoginActivity;
import com.example.wishstar.view.activity.UpdateUserMsgActivity;
import com.example.wishstar.view.adpter.HabitAdapter;
import com.example.wishstar.view.adpter.WishAdapter;
import com.example.wishstar.viewmodel.StarViewModel;
import com.example.wishstar.viewmodel.WishViewModel;
import com.xujiaji.happybubble.BubbleDialog;
import com.xujiaji.happybubble.BubbleLayout;

import java.util.List;

public class WishFragment extends Fragment implements HabitAdapter.OnHabitItemClickListener, UpdateHabitDialogFragment.UpdateHabitCallBack, UpdateWishDialogFragment.UpdateWishCallBack, WishAdapter.OnWishItemClickListener {
    private String TAG = "WishFragment";
    private WishViewModel wishViewModel;
    private FragmentWishBinding fragmentWishBinding;
    private SwipeRefreshLayout swipeRefresh;

    private GridLayoutManager habitManager, wishManger;
    private HabitAdapter habitAdapter;
    private WishAdapter wishAdapter;

    private CustomOperateDialog addDialog;
    private WishFragment wishFragment;
    private UpdateHabitDialogFragment updateHabitDialogFragment;
    private UpdateWishDialogFragment updateWishDialogFragment;

    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {
        wishViewModel = new ViewModelProvider(this).get(WishViewModel.class);
        fragmentWishBinding = FragmentWishBinding.inflate(inflater, container, false);
        View root = fragmentWishBinding.getRoot();

        habitManager = new GridLayoutManager(getContext(), 3, GridLayoutManager.VERTICAL, false);
        fragmentWishBinding.habitRecyclerview.setLayoutManager(habitManager);

        wishManger = new GridLayoutManager(getContext(), 3, GridLayoutManager.VERTICAL, false);
        fragmentWishBinding.wishRecyclerview.setLayoutManager(wishManger);
        wishFragment = this;

        wishViewModel.getHabit().observe(getActivity(), new Observer<List<Habit>>() {
            @Override
            public void onChanged(List<Habit> habits) {
                if (habits.size() >= 0) {
                    Log.e(TAG, "observe-> habits ========================>");
                    for (int i = 0; i < habits.size(); ++i) {
                        Log.e(TAG, habits.get(i).toString());
                    }
                    habitAdapter = new HabitAdapter(habits, getContext());
                    habitAdapter.setOnHabitItemClickListener(WishFragment.this);
                    fragmentWishBinding.habitRecyclerview.setAdapter(habitAdapter);
                }
            }
        });

        wishViewModel.getWish().observe(getActivity(), new Observer<List<Wish>>() {
            @Override
            public void onChanged(List<Wish> wishes) {
                if (wishes.size() >= 0) {
                    Log.e(TAG, "observe -> wishs ========================>");
                    for (int i = 0; i < wishes.size(); ++i) {
                        Log.e(TAG, wishes.get(i).toString());
                    }
                    wishAdapter = new WishAdapter(wishes, getContext());
                    wishAdapter.setOnItemClickListener(WishFragment.this);
                    fragmentWishBinding.wishRecyclerview.setAdapter(wishAdapter);
                }
            }
        });

        swipeRefresh = fragmentWishBinding.wishSwipeRefresh;
        swipeRefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                wishViewModel.refresh();
                swipeRefresh.setRefreshing(false);
            }
        });

        TextView title = root.findViewById(R.id.more_title);
        title.setText(getContext().getString(R.string.title_wish));

        ImageView addImage = root.findViewById(R.id.title_more_image);
        BubbleDialog.Position position = BubbleDialog.Position.BOTTOM;
        BubbleLayout bubbleLayout = new BubbleLayout(getContext());
        bubbleLayout.setBubbleColor(getContext().getColor(R.color.alpha_65_black));

        addImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (addDialog == null) {
                    addDialog = new CustomOperateDialog(getContext(), R.layout.dialog_wish_more)
                            .setPosition(position)
                            .setClickedView(addImage)
                            .calBar(true)
                            .setBubbleLayout(bubbleLayout);
                }
                addDialog.show();
                addDialog.setClickListener(new CustomOperateDialog.OnClickCustomButtonListener() {
                    @Override
                    public void onClick(String str) {
                        Log.e(TAG, "onClick() ->" +  str);
                        if (str.equals("新建习惯")) {
                            Intent intent = new Intent(getActivity(), AddHabitActivity.class);
                            addDialog.dismiss();
                            startActivity(intent);
                        }
                        else if (str.equals("新建愿望")) {
                            Intent intent = new Intent(getActivity(), AddWishActivity.class);
                            addDialog.dismiss();
                            startActivity(intent);
                        }
                    }
                });
            }
        });

        return root;
    }

    @Override
    public void onResume() {
        super.onResume();
        wishViewModel.refresh();

    }

    @Override
    public void onItemClick(Habit habit) {
        Toast.makeText(getContext(), "" + habit.habitTitle, Toast.LENGTH_SHORT).show();
        Log.e(TAG, habit.toString());
        updateHabitDialogFragment = new UpdateHabitDialogFragment();

        Bundle bundle = new Bundle();
        bundle.putInt("habitId", habit.habitId);
        bundle.putString("habitTitle", habit.habitTitle);
        bundle.putInt("habitIcon", habit.habitIcon);
        updateHabitDialogFragment.setArguments(bundle);
        updateHabitDialogFragment.setUpdateHabitCallBack(WishFragment.this);

        updateHabitDialogFragment.show(getFragmentManager(), "updateHabit");
    }

    @Override
    public void onLongItemClick(Habit habit) {
        wishViewModel.deleteHabitByHabitId(habit.habitId);
        Handler handler = new Handler();
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                wishViewModel.refresh();
            }
        }, 500);//0.5秒后执行Runnable中的run方法
    }

    @Override
    public void onItemClick(Wish wish) {
        Toast.makeText(getContext(), "" + wish.wishTitle, Toast.LENGTH_SHORT).show();
        Log.e(TAG, wish.toString());
        updateWishDialogFragment = new UpdateWishDialogFragment();

        Bundle bundle = new Bundle();
        bundle.putInt("wishId", wish.wishId);
        bundle.putString("wishTitle", wish.wishTitle);
        bundle.putInt("wishIcon", wish.wishIcon);
        updateWishDialogFragment.setArguments(bundle);

        updateWishDialogFragment.setUpdateWishCallBack(WishFragment.this);
        updateWishDialogFragment.show(getChildFragmentManager(), "updateWish");
    }

    @Override
    public void onLongItemClick(Wish wish) {
        wishViewModel.deleteWishByWishId(wish.wishId);
        Handler handler = new Handler();
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                wishViewModel.refresh();
            }
        }, 500);//0.5秒后执行Runnable中的run方法
    }

    @Override
    public void updateHabit(UpdateHabit habit) {
        wishViewModel.updateHabit(habit);
        Handler handler = new Handler();
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                wishViewModel.refresh();
            }
        }, 500);//0.5秒后执行Runnable中的run方法
    }

    @Override
    public void updateWish(UpdateWish wish) {
        wishViewModel.updateWish(wish);
        Handler handler = new Handler();
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                wishViewModel.refresh();
            }
        }, 500);//0.5秒后执行Runnable中的run方法
    }

    @Override
    public void delayWish(int wishId, CommonDate date) {
        wishViewModel.delayWish(wishId, date);
        Handler handler = new Handler();
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                wishViewModel.refresh();
            }
        }, 500);//0.5秒后执行Runnable中的run方法
    }
}
